﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace factoryMethod_calculator
{
    // 客户端代码
    class Program
    {
        static void Main(string[] args)
        {
            // 创建加法工厂，用于创建加法操作类
            IOperationFactory additionFactory = new AdditionFactory();
            IOperation addition = additionFactory.CreateOperation();
            Console.WriteLine($"加法结果：{addition.GetOut(5, 3)}");

            // 创建减法工厂，用于创建减法操作类
            IOperationFactory subtractionFactory = new SubtractionFactory();
            IOperation subtraction = subtractionFactory.CreateOperation();
            Console.WriteLine($"减法结果：{subtraction.GetOut(5, 3)}");

            // 创建乘法工厂，用于创建乘法操作类
            IOperationFactory multiplicationFactory = new MultiplicationFactory();
            IOperation multiplication = multiplicationFactory.CreateOperation();
            Console.WriteLine($"乘法结果：{multiplication.GetOut(5, 3)}");

            // 创建除法工厂，用于创建除法操作类
            IOperationFactory divisionFactory = new DivisionFactory();
            IOperation division = divisionFactory.CreateOperation();
            Console.WriteLine($"除法结果：{division.GetOut(6, 3)}");


            Console.WriteLine($"除法结果：{(new DivisionFactory()).CreateOperation().GetOut(6, 5)}");


            Console.ReadLine();
        }
    }


    // 接口- 操作类
    interface IOperation
    {
        double GetOut(double num1, double num2); // 计算方法
    }

    // 具体产品 - 加法操作类
    class Addition : IOperation
    {
        public double GetOut(double num1, double num2)
        {
            return num1 + num2; // 返回两数之和
        }
    }

    // 具体产品 - 减法操作类
    class Subtraction : IOperation
    {
        public double GetOut(double num1, double num2)
        {
            return num1 - num2; // 返回两数之差
        }
    }

    // 具体产品 - 乘法操作类
    class Multiplication : IOperation
    {
        public double GetOut(double num1, double num2)
        {
            return num1 * num2; // 返回两数之积
        }
    }

    // 具体产品 - 除法操作类
    class Division : IOperation
    {
        public double GetOut(double num1, double num2)
        {
            if (num2 == 0)
            {
                throw new ArgumentException("除数不能为0"); // 如果除数为0，则抛出异常
            }
            return num1 / num2; // 返回两数之商
        }
    }



    // 接口 - 工厂接口
    interface IOperationFactory
    {
        //string zhang;
        IOperation CreateOperation(); // 创建操作类的方法
    }

    // 具体工厂 - 加法工厂
    class AdditionFactory : IOperationFactory
    {
        public IOperation CreateOperation()
        {
            return new Addition(); // 创建加法操作类
        }
    }

    // 具体工厂 - 减法工厂
    class SubtractionFactory : IOperationFactory
    {
        public IOperation CreateOperation()
        {
            return new Subtraction(); // 创建减法操作类
        }
    }

    // 具体工厂 - 乘法工厂
    class MultiplicationFactory : IOperationFactory
    {
        public IOperation CreateOperation()
        {
            return new Multiplication(); // 创建乘法操作类
        }
    }

    // 具体工厂 - 除法工厂
    class DivisionFactory : IOperationFactory
    {
        public IOperation CreateOperation()
        {
            return new Division(); // 创建除法操作类
        }
    }

   


}
